<template>
  <div class="video-record">
    <div class="item" v-for="(item,index) in data" :key="index" :style="{
      marginLeft: active === '/' ? '0.08rem' : '0.18rem',
      marginRight: active === '/' ? '0.08rem' : '0.18rem'
    }">
      <div class="name">{{ item.cameraName }}</div>
      <div class="video-box">
        <div class="video">
          <h5-hk-video :pre-url="item.url" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  defaultUrlApi,
  drivingDefaultUrlApi,
  trolleyDefaultUrlApi,
  cameraListApi,
  drivingCameraListApi,
  trolleyCameraListApi
} from '@/api/'
import h5HkVideo from './h5-hk-video'

export default {
  name: 'video-record',
  data() {
    return {
      active: '/',
      data: [],
      timer: null,
    }
  },
  components: {
    h5HkVideo
  },
  computed: {
    api() {
      let url = defaultUrlApi
      const path = this.$route.path
      if (path === '/trolley') {
        url = trolleyDefaultUrlApi
      } else if (path === '/driving') {
        url = drivingDefaultUrlApi
      }
      return url
    },
    cameraApi() {
      let url = cameraListApi
      const path = this.$route.path
      if (path === '/trolley') {
        url = trolleyCameraListApi
      } else if (path === '/driving') {
        url = drivingCameraListApi
      }
      return url
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearTimeout(this.timer)
      this.timer = null
    }
  },
  methods: {
    loadRes() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
      this.data = []
      this.api({ offLimit: this.active === '/' ? 6 : 8 }).then(res => {
        this.data = res.data?.data || []
        this.timer = setTimeout(() => {
          this.loadRes()
        }, 1000 * 60 * 5)
      })
    }
  },
  created() {
    this.active = this.$route.path
    this.loadRes()
  },
  watch: {
    '$route.path'(v) {
      this.active = v
      this.loadRes()
    }
  }
}
</script>

<style lang="scss" scoped>
.video-record {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  padding-top: 0.7rem;
  .item {
    width: 2.68rem;
    margin-bottom: 0.24rem;
    .name {
      font-size: 0.16rem;
      color: #FFFFFF;
      line-height: 0.40rem;
      text-align: center;
    }
    .video-box {
      width: 2.68rem;
      height: 2.72rem;
      background: url('../assets/images/icon_spk.png') center no-repeat;
      background-size: 100% 100%;
      overflow: hidden;
      .video {
        width: 2.4rem;
        height: 2.4rem;
        margin: 0.18rem auto 0;
        background-color: #ddd;
      }
    }
  }
}
</style>